<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>设置我的密码</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layui/layuiAdmin/css/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">修改密码</div>
                <div class="layui-card-body" pad15>

                    <div class="layui-form">
                        <div class="layui-form-item">
                            <label class="layui-form-label">当前密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="oldPassword" lay-verify="required" lay-verType="tips" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">新密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="password" lay-verify="required|pass" lay-verType="tips" autocomplete="off" id="LAY_password" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">确认新密码</label>
                            <div class="layui-input-inline" >
                                <input type="password" name="repassword" lay-verify="repass" lay-verType="tips" autocomplete="off" class="layui-input ">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="LAY-user-upPassword-submit">确认修改</button>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<script src="../../layui/layui.js"></script>
<script>
    layui.config({
        base: '../../layui/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','layer','layedit'], function(){
        var $ = layui.$
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,form = layui.form;

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');
        //自定义验证规则
        form.verify({
            pass: [
                /^[a-zA-Z]\w{5,17}$/
                ,'以字母开头，长度在6~18之间，只能包含字母、数字和下划线'
            ],
            repass:function (value){
                var pass = $("input[name=password]").val();
                if(value != pass){
                    return "两次输入的密码不一致";
                }
            }
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });


        //提交
        form.on('submit(LAY-user-upPassword-submit)', function(obj){
            var param = obj.field;
            var userInfo = JSON.parse(localStorage.getItem("userInfo"))
            // 向原有对象中添加属性及对应的值
            param.userId = userInfo.userId;
            // 把确认密码字段去掉
            param.repassword = undefined;
            // 调用ajax
            $.ajax({
                url:"/sys_user/update_password",
                type:"post",
                data:JSON.stringify(param),
                contentType:"application/json;charset=utf-8",
                dataType:"json",
                success:function (res){
                    if(res.code == 0){
                        localStorage.clear();
                        layer.msg(res.message, {
                            offset: '15px'
                            ,icon: 1
                            ,time: 2000
                        }, function(){
                            window.parent.location.href = '../../login.html';
                        });
                    }else{
                        layer.msg(res.message, {icon: 5});
                    }
                }
            })

        });
    });
</script>
</body>
</html>